//
//  CustomBarView.m
//  CoolTennisBall
//
//  Created by 谢伟康 on 15/12/21.
//  Copyright © 2015年 CoolLang. All rights reserved.
//

#import "CustomBarView.h"

//16是文字高度
#define BarHeight (self.bounds.size.height - 16 - 13 - 10)

@interface CustomBarView ()

@property (nonatomic,strong) NSArray *barHeightArray;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pingJiHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xuanQiuHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xueQiuHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *jieJiHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *kouShaHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *faQiuHeight;

@property (weak, nonatomic) IBOutlet UILabel *flatDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *sliceDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *topspinDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *volleyDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *smashDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *serveDescriptionLabel;

@end

@implementation CustomBarView

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    _barHeightArray = @[_pingJiHeight,_xuanQiuHeight,_xueQiuHeight,_jieJiHeight,_kouShaHeight,_faQiuHeight];
    for (NSInteger index = 0; index < 6; index++) {
        UIImageView *imageView = (UIImageView *)[self viewWithTag:index + 10];
        imageView.image = [imageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(12, 0, 1, 0)];
    }
    
    self.serveDescriptionLabel.text = NSLocalizedString(@"Serve", nil);
    self.smashDescriptionLabel.text = NSLocalizedString(@"Smash", nil);
    self.volleyDescriptionLabel.text = NSLocalizedString(@"Volley", nil);
    self.sliceDescriptionLabel.text = NSLocalizedString(@"Slice", nil);
    self.topspinDescriptionLabel.text = NSLocalizedString(@"Topspin", nil);
    self.flatDescriptionLabel.text = NSLocalizedString(@"Flat", nil);
}

- (void)configureBarHeightWithValueArr:(NSArray *)valueArray
{
    NSInteger totalSwings = 0;
    for (NSNumber *number in valueArray) {
        totalSwings += [number integerValue];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        NSInteger index = 0;
        for (NSNumber * value in valueArray) {
            NSLayoutConstraint * barHeightConstraint = self.barHeightArray[index];
            //13是基础高度
            if (totalSwings != 0) {
                barHeightConstraint.constant = 13 + [value integerValue]/(CGFloat)totalSwings * BarHeight;
            }
            index++;
        }
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (IBAction)clickBarView:(id)sender {
    UIButton *button = (UIButton *)sender;
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(clickBarViewFrame:AtIndex:)]) {
        [self.delegate clickBarViewFrame:button.frame AtIndex:button.tag - 100];
    }
}

@end
